#!/usr/bin/php
<?php
error_reporting(E_ERROR | E_PARSE);

$visitors = array("1" => "111111111", "2" => "222222222", "3" => "333333333", "4" => "444444444", "5" => "555555555", "6" => "666666666", "7" => "777777777");
$site_area = array("1" => " 3 11", "2" => " 4 12", "3" => " 5 13", "4" => " 6 14", "5" => " 7 15", "6" => " 8 16", "7" => " 9 17");

$DateStamp = date("Y-m-d");
// echo "Datestamp: ".$DateStamp."\n";
$TestTime = strtotime ($DateStamp);
// echo "Testtime: ".$TestTime."\n";
$i=1;
while($i<=14){
	$testtime[$i] = $TestTime - ((86400)*$i);
	$datestamp[$i] = date("Y-m-d",$testtime[$i]);
	$datadir[$i] = "/shareddata/customer/canary/sampling/".$datestamp[$i]."/";
	$myDirectory[$i] = opendir($datadir[$i]);
	while($entryName[$i] = readdir($myDirectory[$i])) {
//		echo "File: ".$entryName[$i]."\n";
        $dirArray[$i][] = $entryName[$i]; }
	$indexCount[$i] = count($dirArray[$i]);
//	echo "testtime[$i]: ".$testtime[$i]."\n";
//	echo "Datestamp: ".$datestamp[$i]."\n";
//	echo "Directory: ".$datadir[$i]."\n";
//	echo "Num Files: ".$indexCount[$i]."\n";
//	echo "    Files: \n"; print_r($dirArray[$i]); echo "\n";
	$i++; }

$full_date = date('l, F j, Y', $TestTime);
echo "\n  Querying CANARY Shared Data for: ".$full_date."\n\n";

$i=1;
$z=1;
while($i <= 7){
	$x=0;
	$y=1;
	if(($visitors[$i] == "111111111") || ($visitors[$i] == "222222222")) {
		$z=1;
		echo "  Visitor: ".$visitors[$i]."\n";
		while($y <= 3){
			$x=0;
			while($x < $indexCount[$z]){
//				echo "Filename[$y][$x] - $indexCount[$z]: ".$dirArray[$y][$x]."\n";
				$commandline[$x] = "zcat ".$datadir[$y].$dirArray[$y][$x]." | sed -e 's/ /_/g' | sed -e 's/,/ /g' | sed -e 's/,,/ /g' | awk '{print $2,$16,$11}' | grep \"".$visitors[$i].$site_area[$i]."\"  | wc -l ";
//				echo "Commandline: ".$commandline[$x]."\n";
				$number = exec($commandline[$x]);
//				echo "Number: ".$number."\n";
				echo "     ".$dirArray[$y][$x].": ".$number."\n";
				if($visitors[$i] == "111111111"){
					$v1_number = $v1_number+$number; }
				else if($visitors[$i] == "222222222"){
					$v2_number = $v2_number+$number; }
				$x++;
			}
		$y++;
		$z++;
		}
	}
	else if(($visitors[$i] == "333333333") || ($visitors[$i] == "444444444")) {
		$z=1;
		echo "  Visitor: ".$visitors[$i]."\n";
		while($y <= 4){
			$x=0;
			while($x < $indexCount[$z]){
//				echo "Filename[$y][$x] - $indexCount[$z]: ".$dirArray[$y][$x]."\n";
				$commandline[$x] = "zcat ".$datadir[$y].$dirArray[$y][$x]." | sed -e 's/ /_/g' | sed -e 's/,/ /g' | sed -e 's/,,/ /g' | awk '{print $2,$16,$11}' | grep \"".$visitors[$i].$site_area[$i]."\"  | wc -l ";
//				echo "Commandline: ".$commandline[$x]."\n";
				$number = exec($commandline[$x]);
//				echo "Number: ".$number."\n";
				echo "     ".$dirArray[$y][$x].": ".$number."\n";
				if($visitors[$i] == "333333333"){
					$v3_number = $v3_number+$number; }
				else if($visitors[$i] == "444444444"){
					$v4_number = $v4_number+$number; }
				$x++;
			}
		$y++;
		$z++;
		}
	}
	else if(($visitors[$i] == "555555555") || ($visitors[$i] == "666666666")) {
		$z=1;
		echo "  Visitor: ".$visitors[$i]."\n";
		while($y <= 7){
			$x=0;
			while($x < $indexCount[$z]){
//				echo "Filename[$y][$x] - $indexCount[$z]: ".$dirArray[$y][$x]."\n";
				$commandline[$x] = "zcat ".$datadir[$y].$dirArray[$y][$x]." | sed -e 's/ /_/g' | sed -e 's/,/ /g' | sed -e 's/,,/ /g' | awk '{print $2,$16,$11}' | grep \"".$visitors[$i].$site_area[$i]."\"  | wc -l ";
//				echo "Commandline: ".$commandline[$x]."\n";
				$number = exec($commandline[$x]);
//				echo "Number: ".$number."\n";
				echo "     ".$dirArray[$y][$x].": ".$number."\n";
				if($visitors[$i] == "555555555"){
					$v5_number = $v5_number+$number; }
				else if($visitors[$i] == "666666666"){
					$v6_number = $v6_number+$number; }
				$x++;
			}
		$y++;
		$z++;
		}
	}
	else if($visitors[$i] == "777777777") {
		$z=1;
		echo "  Visitor: ".$visitors[$i]."\n";
		while($y <= 14){
			$x=0;
			while($x < $indexCount[$z]){
//				echo "Filename[$y][$x] - $indexCount[$z]: ".$dirArray[$y][$x]."\n";
				$commandline[$x] = "zcat ".$datadir[$y].$dirArray[$y][$x]." | sed -e 's/ /_/g' | sed -e 's/,/ /g' | sed -e 's/,,/ /g' | awk '{print $2,$16,$11}' | grep \"".$visitors[$i].$site_area[$i]."\"  | wc -l ";
//				echo "Commandline: ".$commandline[$x]."\n";
				$number = exec($commandline[$x]);
//				echo "Number: ".$number."\n";
				echo "     ".$dirArray[$y][$x].": ".$number."\n";
				$v7_number = $v7_number+$number; 
				$x++;
			}
		$y++;
		$z++;
		}
	}

	
$i++;
}
		echo "\n  SUMMARY \n";
		echo "  Visitor 1: Registered ".$v1_number." impressions that matched SITE.ID=3 and AREA.ID=11 over the last 3 days.\n";
		if($v1_number < 2){
			echo "     The trait for this visitor should be NONE\n"; }
		else if (($v1_number >= 2)&&($v1_number < 5)){
			echo "     The trait for this visitor should be LOW\n"; }
		else if (($v1_number >= 5)&&($v1_number < 8)){
			echo "     The trait for this visitor should be MEDIUM\n"; }
		else if ($v1_number >= 8){
			echo "     The trait for this visitor should be HIGH\n"; }
		echo "  -------------------------\n";
		echo "  Visitor 2: Registered ".$v2_number." impressions that matched SITE.ID=4 and AREA.ID=12 over the last 3 days.\n";
		if($v2_number < 2){
			echo "     The trait for this visitor should be NONE\n"; }
		else if (($v2_number >= 2)&&($v2_number < 5)){
			echo "     The trait for this visitor should be LOW\n"; }
		else if (($v2_number >= 5)&&($v2_number < 8)){
			echo "     The trait for this visitor should be MEDIUM\n"; }
		else if ($v2_number >= 8){
			echo "     The trait for this visitor should be HIGH\n"; }
		echo "  -------------------------\n";
		echo "  Visitor 3: Registered ".$v3_number." impressions that matched SITE.ID=5 and AREA.ID=13 over the last 4 days.\n";
		if($v3_number < 2){
			echo "     The trait for this visitor should be NONE\n"; }
		else if (($v3_number >= 2)&&($v3_number < 5)){
			echo "     The trait for this visitor should be LOW\n"; }
		else if (($v3_number >= 5)&&($v3_number < 8)){
			echo "     The trait for this visitor should be MEDIUM\n"; }
		else if ($v3_number >= 8){
			echo "     The trait for this visitor should be HIGH\n"; }
		echo "  -------------------------\n";
		echo "  Visitor 4: Registered ".$v4_number." impressions that matched SITE.ID=6 and AREA.ID=14 over the last 4 days.\n";
		if($v4_number < 2){
			echo "     The trait for this visitor should be NONE\n"; }
		else if (($v4_number >= 2)&&($v4_number < 5)){
			echo "     The trait for this visitor should be LOW\n"; }
		else if (($v4_number >= 5)&&($v4_number < 8)){
			echo "     The trait for this visitor should be MEDIUM\n"; }
		else if ($v4_number >= 8){
			echo "     The trait for this visitor should be HIGH\n"; }
		echo "  -------------------------\n";
		echo "  Visitor 5: Registered ".$v5_number." impressions that matched SITE.ID=7 and AREA.ID=15 over the last 7 days.\n";
		if($v5_number < 6){
			echo "     The trait for this visitor should be NONE\n"; }
		else if (($v5_number >= 6)&&($v5_number < 12)){
			echo "     The trait for this visitor should be LOW\n"; }
		else if (($v5_number >= 12)&&($v5_number < 20)){
			echo "     The trait for this visitor should be MEDIUM\n"; }
		else if ($v5_number >= 20){
			echo "     The trait for this visitor should be HIGH\n"; }
		echo "  -------------------------\n";
		echo "  Visitor 6: Registered ".$v6_number." impressions that matched SITE.ID=8 and AREA.ID=16 over the last 7 days.\n";
		if($v6_number < 6){
			echo "     The trait for this visitor should be NONE\n"; }
		else if (($v6_number >= 6)&&($v6_number < 12)){
			echo "     The trait for this visitor should be LOW\n"; }
		else if (($v6_number >= 12)&&($v6_number < 20)){
			echo "     The trait for this visitor should be MEDIUM\n"; }
		else if ($v6_number >= 20){
			echo "     The trait for this visitor should be HIGH\n"; }
		echo "  -------------------------\n";
		echo "  Visitor 7: Registered ".$v7_number." impressions that matched SITE.ID=9 and AREA.ID=17 over the last 14 days.\n";
		if($v7_number < 6){
			echo "     The trait for this visitor should be NONE\n"; }
		else if (($v7_number >= 6)&&($v7_number < 12)){
			echo "     The trait for this visitor should be LOW\n"; }
		else if (($v7_number >= 12)&&($v7_number < 20)){
			echo "     The trait for this visitor should be MEDIUM\n"; }
		else if ($v7_number >= 20){
			echo "     The trait for this visitor should be HIGH\n"; }

?>

